package gemini.castor.ui.client.page.content.home.search.form;

import gemini.basic.dto.SearchDistributorCriteria;
import gemini.basic.dto.SearchDistributorResult;
import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.widget.ErrorWidget;
import gemini.castor.ui.client.page.widget.NumberFieldStandard;
import gemini.castor.ui.client.page.widget.TextFieldTextNumber;
import gemini.castor.ui.client.utils.UiConstants;

import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.ColumnFormatter;
import com.google.gwt.user.client.ui.HTMLTable.RowFormatter;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class SearchFormView extends Composite implements
		SearchFormPresenter.Display {

	@UiTemplate("SearchFormView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, SearchFormView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);
	private I18nConstants constants;

	public SearchFormView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}

	@UiField
	ErrorWidget cnwError;

	@UiField
	TextFieldTextNumber tbxDistributorId;

	@UiField
	TextBox tbxLastName;
	
	@UiField
	TextBox tbxFirstName;

	@UiField
	NumberFieldStandard tbxPhone;

	@UiField
	TextBox tbxEmail;
	
	@UiField
	HTML htmPhone;

	@UiField
	HTML htmDistributorID;

	@UiField
	HTML htmFullName;
	
	@UiField
	HTML htmEmail;
	
	@UiField
	VerticalPanel pnlDetail;

	@UiField
	CheckBox chxExactly;

	@UiField
	Button btnSearch;

	@UiField
	Button btnReset;

	@UiField
	FlexTable ftbResult;

	@UiHandler("btnReset")
	void onReset(ClickEvent e) {
		reset();
	}
	
	@Override
	public boolean isValidate(){
		resetStyle();
		boolean result = true;
		if (tbxDistributorId.getValue().isEmpty()
				&& tbxEmail.getValue().isEmpty()
				&& tbxLastName.getValue().isEmpty()
				&& tbxFirstName.getValue().isEmpty()
				&& tbxPhone.getValue().isEmpty()) {
			result = false;
			showError();
		}
		return result;
		
	}
	
	private void showError() {
		tbxDistributorId.addStyleDependentName("error");
		tbxEmail.addStyleDependentName("error");
		tbxFirstName.addStyleDependentName("error");
		tbxLastName.addStyleDependentName("error");
		tbxPhone.addStyleDependentName("error");
		
		cnwError.setMessage(constants.stringRequireOnField());
		cnwError.setVisible(true);
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public HasClickHandlers getSearchClickHandlers() {
		return btnSearch;
	}

	@Override
	public void loadData() {
		resetResult();
	}
	
	@Override
	public void showResult(List<SearchDistributorResult> resultPersons){
		resetResult();
		
		if (resultPersons == null || resultPersons.isEmpty()) {
			cnwError.setMessage(constants.htmNoResultFound());
			cnwError.setVisible(true);
		} else {
			ftbResult.setVisible(true);
			ColumnFormatter colFmt = ftbResult.getColumnFormatter();
			
			colFmt.setWidth(0, "8%");
			colFmt.setWidth(1, "42%");
			colFmt.setWidth(2, "25%");
			colFmt.setWidth(3, "25%");
			
			// Column title
			ftbResult.setHTML(0, 0, UiConstants.TAG_BOLB + constants.htmNb() + UiConstants.TAG_BOLB_END);
			ftbResult.setHTML(0, 1, UiConstants.TAG_BOLB + constants.htmFullName() + UiConstants.TAG_BOLB_END);
			ftbResult.setHTML(0, 2, UiConstants.TAG_BOLB + constants.htmPhone() + UiConstants.TAG_BOLB_END);
			ftbResult.setHTML(0, 3, UiConstants.TAG_BOLB + constants.htmDistributorCode() + UiConstants.TAG_BOLB_END);
			ftbResult.getRowFormatter().setStyleName(0, "tableHeader");
			ftbResult.setStyleName("ftbSummary");
			
			int i = 1;
			HTML fullName = null;
			for (final SearchDistributorResult ps : resultPersons) {
				RowFormatter rowFmt = ftbResult.getRowFormatter();
				ftbResult.setHTML(i, 0, String.valueOf(i));
				
				fullName = new HTML();
				fullName.setHTML(ps.getFullName());
				fullName.setStyleName("productLink");
				fullName.addClickHandler(new ClickHandler() {
					
					@Override
					public void onClick(ClickEvent event) {
						showDetail(ps);
					}
				});
				
				ftbResult.setWidget(i, 1, fullName);
				ftbResult.setHTML(i, 2, ps.getPhoneNumber());
				ftbResult.setHTML(i, 3, ps.getDistributorId());
				if(i%2 == 0){
					rowFmt.setStyleName(i, "rowEven");
				}
				
				i++;
			}
		}
	}
	
	private void showDetail(SearchDistributorResult rs){
		String fullName = rs.getFullName();
		String fullPhone = rs.getPhoneNumber();
		String fullEmail = rs.getEmail();
		
		if (rs.getSpouseFullName() != null && !rs.getSpouseFullName().isEmpty()) {
			fullName += " & " + rs.getSpouseFullName();
			//fullPhone += " / " + rs.getSpousePhoneNumber();
			//fullEmail += " / " + rs.getSpouseEmail();
		}
		
		htmDistributorID.setHTML(rs.getDistributorId());
		htmPhone.setHTML(fullPhone);
		htmEmail.setHTML(fullEmail);
		htmFullName.setHTML(fullName);
		
		pnlDetail.setVisible(true);
	}
	
	public void resetResult(){
		ftbResult.removeAllRows();
		ftbResult.setVisible(false);
		pnlDetail.setVisible(false);
	}
	
	private void reset() {
		resetValue();
		resetStyle();
	}

	private void resetValue() {
		tbxDistributorId.setValue("", true);
		tbxEmail.setValue("", true);
		tbxLastName.setValue("", true);
		tbxFirstName.setValue("", true);
		tbxPhone.setValue("", true);
		
		chxExactly.setValue(false);
	}
	
	private void resetStyle() {
		tbxDistributorId.removeStyleDependentName("error");
		tbxEmail.removeStyleDependentName("error");
		tbxLastName.removeStyleDependentName("error");
		tbxFirstName.removeStyleDependentName("error");
		tbxPhone.removeStyleDependentName("error");
		
		cnwError.setVisible(false);
	}
	

	@Override
	public SearchDistributorCriteria getCriteria() {
		SearchDistributorCriteria result = new SearchDistributorCriteria();
		result.setDistributorId(tbxDistributorId.getValue());
		result.setEmail(tbxEmail.getValue());
		result.setPhoneNumber(tbxPhone.getValue());
		result.setFirstName(tbxFirstName.getValue());
		result.setLastName(tbxLastName.getValue());
		result.setExactly(chxExactly.getValue());
		return result;
	}

}
